package com.site.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class TextWrapper extends HttpServletRequestWrapper {
    private Map<String ,String> map = new HashMap<>();

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public TextWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getParameter(String name) {
        String newValue = super.getParameter(name);
        //模拟map脏文数据
        map.put("你我","***");
        map.put("测试","***");
        map.put("<","&lt;");
        map.put(">","&gt;");
        map.put("'","&quat");

        //循环替换
        Set<String> keySet = map.keySet();
        Iterator<String> keys = keySet.iterator();
        while (keys.hasNext()){
            String key = keys.next();
            if(key.equals("")||key==null||name.equals("")||name==null){
                continue;
            } else if (super.getParameter(name).contains(key)){
                newValue = newValue.replace(key,map.get(key));
            }
        }

        System.out.println("老值"+super.getParameter(name)+",新值"+newValue);
        return newValue;
    }
}
